package com.huikeportal.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.huikeportal.controller.common.Lang;
import com.huikeportal.controller.common.Tools;
import com.huikeportal.model.ChangeGcParam;
import com.huikeportal.model.Language;
import com.huikeportal.model.RegionModel;
import com.huikeportal.model.SiteAllInfo;
import com.huikeportal.model.SiteCommon;
import com.huikeportal.model.SiteSimpleInfo;
import com.huikeportal.model.SquareChange;
import com.huikeportal.model.UserInfo;
import com.huikeportal.model.WordBook;
import com.huikeportal.model.common.IndexPermission;
import com.huikeportal.model.common.SqlParameter;
import com.huikeportal.service.CommonService;
import com.huikeportal.service.FootfallGlobalService;
import com.huikeportal.service.LoginService;
import com.huikeportal.service.OverViewService;
import com.huikeportal.service.SelectSiteService;

@Controller
public class SingleSquareHotStore {
	@Autowired
	private HttpSession session;

	@Autowired
	private LoginService loginService;

	@Autowired
	private OverViewService overViewService;

	@Autowired
	private FootfallGlobalService footfallGlobalService;

	@Autowired
	private SelectSiteService selectSiteService;

	@Autowired
	private CommonService commonService;

	@RequestMapping("/singleSquareHotStore")
	public String shopView(String regionSiteId, String floorSiteId,
			String siteId, ModelMap model) throws Exception {
		RegionModel regionModel = new RegionModel();
		if (siteId != null && !"".equals(siteId)) {
			regionModel.setGcSiteId(siteId);
		}
		String sysLang = session.getAttribute("sysLang").toString();
		Language lang = Lang.getInfo(sysLang);
		model.addAttribute("lang", lang);
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		String menuHtml = loginService.getMenuHtml(userInfo, "singleSquareHotStore");
		model.addAttribute("menuHtml", menuHtml);// 左侧菜单html
		// 获取用户信息
		userInfo = loginService.getUserInfo(userInfo);
		// 获取用户根据店铺反查的广场权限
		List<SiteAllInfo> dpGcList = loginService.getDpGcSiteList(userInfo);
		// 获取用户单独广场权限
		List<SiteAllInfo> gcList = loginService.getGcSiteList(userInfo);
		// 获取用户单独店铺权限
		List<SiteAllInfo> jinDpList = loginService.getJinDpSiteList(userInfo);
		// 如果没有权限
		if (dpGcList.size() == 0) {
			return "noPermission";
		}
		// 如果是第一次进来，广场为空
		if (regionModel.getGcSiteId() == null
				|| "".equals(regionModel.getGcSiteId())) {
			// 从session中获取上一次选中的广场，如果没有，从历史表中获取上一次选中的广场，如果没有，则获取第一家广场，并存入历史表，存入session
			if (session.getAttribute("userSiteSession") != null
					&& !"".equals(session.getAttribute("userSiteSession"))) {
				regionModel.setGcSiteId(session.getAttribute("userSiteSession")
						.toString());
			}
			// 如果session中不存在
			else {
				// 从历史表中获取上一次选中的广场
				String lastSiteId = overViewService.getLastSiteId(userInfo,
						"footfallWhole", sysLang);
				// 如果存在
				if (!"".equals(lastSiteId)) {
					// 判断当前场所权限是否存在

					regionModel.setGcSiteId(lastSiteId);
					// 存入session
					session.setAttribute("userSiteSession", lastSiteId);
				}
				// 存入历史表，存入session
				else {
					// 获取第一家广场
					String gcSiteId = "";
					// 如果是从广场权限中选中的广场
					if (gcList.size() != 0) {
						gcSiteId = gcList.get(0).getSiteid();
					}
					// 如果是通过店铺反查选中的广场,sql中需加入店铺范围
					else {
						gcSiteId = dpGcList.get(0).getSiteid();
					}
					regionModel.setGcSiteId(gcSiteId);
					// 存入历史表
					overViewService.insertLastSiteId(userInfo, "footfallWhole",
							gcSiteId, sysLang);
					// 存入session
					session.setAttribute("userSiteSession", gcSiteId);
				}
			}
		} else {
			// 更新历史表
			overViewService.updateLastSiteId(userInfo, "footfallWhole",
					regionModel.getGcSiteId(), sysLang);
			// 更新session
			session.setAttribute("userSiteSession", regionModel.getGcSiteId());
		}
		/** 如果广场不是有权限的广场，则加入店铺过滤器 **/
		if (!Tools.isHaveGcPer(gcList, regionModel.getGcSiteId())) {
			regionModel.setDpString(Tools.pinjieId(jinDpList));
		}
		/** 更换广场初始化 **/
		// 初始化默认广场信息
		SiteCommon siteCommon = selectSiteService.querySiteInfo(regionModel
				.getGcSiteId());
		model.addAttribute("siteCommon", siteCommon);
		// 更换广场div初始化数据
		ChangeGcParam changeGcParam = new ChangeGcParam();
		changeGcParam.setSelectType("0");// 单选广场
		changeGcParam.setGcString(siteCommon.getSiteId());// 已经选择的广场id
		List<SiteAllInfo> yuanGcList = loginService.getGcSiteList(userInfo);
		SquareChange squareChange = selectSiteService.squareChangeLoad(
				changeGcParam, dpGcList, yuanGcList);// 获取初始化更换广场信息
		model.addAttribute("squareChange", squareChange);
		model.addAttribute("selectType", changeGcParam.getSelectType());
		model.addAttribute("backPage", "singleSquareHotStore");
		/** 更换广场初始化 **/
		// 拼接下拉框
		List<SiteAllInfo> leftList = new ArrayList<SiteAllInfo>();
		List<SiteSimpleInfo> rightGcList = new ArrayList<SiteSimpleInfo>();
		List<SiteSimpleInfo> rightQyList = new ArrayList<SiteSimpleInfo>();
		List<SiteSimpleInfo> rightLcList = new ArrayList<SiteSimpleInfo>();
		List<SiteSimpleInfo> rightDpList = new ArrayList<SiteSimpleInfo>();
		SiteAllInfo site = new SiteAllInfo();
		List<SiteSimpleInfo> gcDpList = overViewService.getComboResult(
				regionModel, "8", userInfo);
		List<SiteSimpleInfo> qyDpList = overViewService.getComboResult(
				regionModel, "9", userInfo);
		List<SiteSimpleInfo> lcDpList = overViewService.getComboResult(
				regionModel, "10", userInfo);
		List<SiteSimpleInfo> dpDpList = overViewService.getComboResult(
				regionModel, "11", userInfo);
		if (gcDpList.size() != 0) {
			site = new SiteAllInfo();
			site.setSiteid("300");
			site.setSitename(lang.getSquare());
			leftList.add(site);
			rightGcList = gcDpList;
		}
		if (qyDpList.size() != 0) {
			site = new SiteAllInfo();
			site.setSiteid("400");
			site.setSitename(lang.getDomain());
			leftList.add(site);
			rightQyList = qyDpList;
		}
		if (lcDpList.size() != 0) {
			site = new SiteAllInfo();
			site.setSiteid("500");
			site.setSitename(lang.getFloor());
			leftList.add(site);
			rightLcList = lcDpList;
		}
		if (dpDpList.size() != 0) {
			site = new SiteAllInfo();
			site.setSiteid("600");
			site.setSitename(lang.getStore());
			leftList.add(site);
			rightDpList = dpDpList;
		}
		model.addAttribute("leftList", leftList);
		model.addAttribute("rightGcList", JSONArray.fromObject(rightGcList)
				.toString());
		model.addAttribute("rightQyList", JSONArray.fromObject(rightQyList)
				.toString());
		model.addAttribute("rightLcList", JSONArray.fromObject(rightLcList)
				.toString());
		model.addAttribute("rightDpList", JSONArray.fromObject(rightDpList)
				.toString());
		// 获取用户指标权限
		List<String> indexList = footfallGlobalService.getUserIndex(userInfo);
		IndexPermission index = Tools.initIndex(indexList, sysLang);
		model.addAttribute("indexList", index);// 用于标题栏动态显示
		model.addAttribute("index", JSONArray.fromObject(index).toString());// 用户js拼接数据动态显示
		// 获取用户按钮权限
		// 查询当前页面id
		SqlParameter sql = new SqlParameter();
		sql.setSysLang(sysLang);
		sql.setString("shopView");
		Integer menuId = selectSiteService.getMenuId(sql);
		// 获取用户权限按钮信息
		List<WordBook> buttonList = loginService.getUserButtonList(sysLang,
				userInfo.getUserId(), menuId);
		String isHaveExport = "0";
		for (int i = 0; i < buttonList.size(); i++) {
			if ("8".equals(buttonList.get(i).getDicCode())) {
				isHaveExport = "1";
				break;
			}
		}
		model.addAttribute("isHaveExport", isHaveExport);
		if (regionSiteId != null && !"".equals(regionSiteId)) {
			model.addAttribute("regionSiteId", regionSiteId);
		}
		if (floorSiteId != null && !"".equals(floorSiteId)) {
			model.addAttribute("floorSiteId", floorSiteId);
		}
		return "singleSquareHotStore";
	}
}
